package org.blog.service.impl;

import org.blog.dao.ArticleMapper;
import org.blog.dao.CommentsMapper;
import org.blog.entity.Article;
import org.blog.entity.Comments;
import org.blog.entity.param.ArticleParam;
import org.blog.entity.vo.ArticleVO;
import org.blog.entity.vo.CommentsVO;
import org.blog.entity.vo.NewArticleVO;
import org.blog.service.ArticleService;
import org.blog.utils.ObjectToMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {


    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CommentsMapper commentsMapper;


    @Override
    public boolean create(Article article) {
        int insert = articleMapper.insertSelective(article);
        if (insert > 0) {

            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean delete(Integer articleId) {
        int i = articleMapper.deleteByPrimaryKey(articleId);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<ArticleVO> search(ArticleParam param) {
        return null;
    }

    @Override
    public boolean update(Article article) {
        return false;
    }

    @Override
    public int getViewNumber(Integer articleId) {
        return 0;
    }

    @Override
    public int addViewNumber(Integer articleId) {
        return articleMapper.addViewNum(articleId);
    }

    @Override
    public List<Article> getHotArticles(Integer articlesLimit) {
        List<Article> hotArticles = articleMapper.getHotArticles(articlesLimit);
        return hotArticles;
    }

    @Override
    public List<NewArticleVO> getNewArticles(Integer articlesLimit) {
        List<NewArticleVO> newArticles = articleMapper.getNewArticles(articlesLimit);
        return newArticles;
    }

    @Override
    public List<Article> getArticles(ArticleParam param) {
        Map<String, Object> stringObjectMap = null;
        try {
            stringObjectMap = ObjectToMap.convertBeanToMap(param);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        List<Article>  list = articleMapper.getArticles(stringObjectMap);
        return list;
    }

    @Override
    public Article getArticleById(Integer articleId) {
        return articleMapper.selectByPrimaryKey(articleId);
    }

    @Override
    public ArticleVO getArticleFromId(Integer articleId) {
        ArticleVO articleVO = articleMapper.getArticleVOByArticleId(articleId);
        List<CommentsVO> comments = commentsMapper.getCommentsVOByArticleId(articleId);
        articleVO.setComments(comments);
        return articleVO;
    }

    @Override
    public List<ArticleVO> getArticleVO(ArticleParam param) {
        try {
            return articleMapper.getArticleVO(ObjectToMap.convertBeanToMap(param));
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
